
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>9.6 使用Python CFFI混合C，C++，Fortran和Python · 《CMake Cookbook》中文版</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter10/10.0-chinese.html" />
    
    
    <link rel="prev" href="9.5-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../preface/preface-chinese.html">
            
                <a href="../preface/preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter0/0.0-chinese.html">
            
                <a href="../chapter0/0.0-chinese.html">
            
                    
                    第0章 配置环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter0/0.1-chinese.html">
            
                <a href="../chapter0/0.1-chinese.html">
            
                    
                    0.1 获取代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter0/0.2-chinese.html">
            
                <a href="../chapter0/0.2-chinese.html">
            
                    
                    0.2 Docker镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter0/0.3-chinese.html">
            
                <a href="../chapter0/0.3-chinese.html">
            
                    
                    0.3 安装必要的软件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter0/0.4-chinese.html">
            
                <a href="../chapter0/0.4-chinese.html">
            
                    
                    0.4 测试环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter0/0.5-chinese.html">
            
                <a href="../chapter0/0.5-chinese.html">
            
                    
                    0.5 上报问题并提出改进建议
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 从可执行文件到库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 将单个源文件编译为可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 切换生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 构建和链接静态库和动态库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 用条件句控制编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 向用户显示选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter1/1.6-chinese.html">
            
                <a href="../chapter1/1.6-chinese.html">
            
                    
                    1.6 指定编译器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter1/1.7-chinese.html">
            
                <a href="../chapter1/1.7-chinese.html">
            
                    
                    1.7 切换构建类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter1/1.8-chinese.html">
            
                <a href="../chapter1/1.8-chinese.html">
            
                    
                    1.8 设置编译器选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter1/1.9-chinese.html">
            
                <a href="../chapter1/1.9-chinese.html">
            
                    
                    1.9 为语言设定标准
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter1/1.10-chinese.html">
            
                <a href="../chapter1/1.10-chinese.html">
            
                    
                    1.10 使用控制流
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter2/2.0-chinese.html">
            
                <a href="../chapter2/2.0-chinese.html">
            
                    
                    第2章 检测环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter2/2.1-chinese.html">
            
                <a href="../chapter2/2.1-chinese.html">
            
                    
                    2.1 检测操作系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter2/2.2-chinese.html">
            
                <a href="../chapter2/2.2-chinese.html">
            
                    
                    2.2 处理与平台相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter2/2.3-chinese.html">
            
                <a href="../chapter2/2.3-chinese.html">
            
                    
                    2.3 处理与编译器相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter2/2.4-chinese.html">
            
                <a href="../chapter2/2.4-chinese.html">
            
                    
                    2.4 检测处理器体系结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter2/2.5-chinese.html">
            
                <a href="../chapter2/2.5-chinese.html">
            
                    
                    2.5 检测处理器指令集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter2/2.6-chinese.html">
            
                <a href="../chapter2/2.6-chinese.html">
            
                    
                    2.6 为Eigen库使能向量化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter3/3.0-chinese.html">
            
                <a href="../chapter3/3.0-chinese.html">
            
                    
                    第3章 检测外部库和程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter3/3.1-chinese.html">
            
                <a href="../chapter3/3.1-chinese.html">
            
                    
                    3.1 检测Python解释器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter3/3.2-chinese.html">
            
                <a href="../chapter3/3.2-chinese.html">
            
                    
                    3.2 检测Python库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter3/3.3-chinese.html">
            
                <a href="../chapter3/3.3-chinese.html">
            
                    
                    3.3 检测Python模块和包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter3/3.4-chinese.html">
            
                <a href="../chapter3/3.4-chinese.html">
            
                    
                    3.4 检测BLAS和LAPACK数学库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter3/3.5-chinese.html">
            
                <a href="../chapter3/3.5-chinese.html">
            
                    
                    3.5 检测OpenMP的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter3/3.6-chinese.html">
            
                <a href="../chapter3/3.6-chinese.html">
            
                    
                    3.6 检测MPI的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter3/3.7-chinese.html">
            
                <a href="../chapter3/3.7-chinese.html">
            
                    
                    3.7 检测Eigen库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter3/3.8-chinese.html">
            
                <a href="../chapter3/3.8-chinese.html">
            
                    
                    3.8 检测Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../chapter3/3.9-chinese.html">
            
                <a href="../chapter3/3.9-chinese.html">
            
                    
                    3.9 检测外部库:Ⅰ. 使用pkg-config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../chapter3/3.10-chinese.html">
            
                <a href="../chapter3/3.10-chinese.html">
            
                    
                    3.10 检测外部库:Ⅱ. 自定义find模块
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter4/4.0-chinese.html">
            
                <a href="../chapter4/4.0-chinese.html">
            
                    
                    第4章 创建和运行测试
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter4/4.1-chinese.html">
            
                <a href="../chapter4/4.1-chinese.html">
            
                    
                    4.1 创建一个简单的单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter4/4.2-chinese.html">
            
                <a href="../chapter4/4.2-chinese.html">
            
                    
                    4.2 使用Catch2库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter4/4.3-chinese.html">
            
                <a href="../chapter4/4.3-chinese.html">
            
                    
                    4.3 使用Google Test库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter4/4.4-chinese.html">
            
                <a href="../chapter4/4.4-chinese.html">
            
                    
                    4.4 使用Boost Test进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter4/4.5-chinese.html">
            
                <a href="../chapter4/4.5-chinese.html">
            
                    
                    4.5 使用动态分析来检测内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter4/4.6-chinese.html">
            
                <a href="../chapter4/4.6-chinese.html">
            
                    
                    4.6 预期测试失败
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter4/4.7-chinese.html">
            
                <a href="../chapter4/4.7-chinese.html">
            
                    
                    4.7 使用超时测试运行时间过长的测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../chapter4/4.8-chinese.html">
            
                <a href="../chapter4/4.8-chinese.html">
            
                    
                    4.8 并行测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../chapter4/4.9-chinese.html">
            
                <a href="../chapter4/4.9-chinese.html">
            
                    
                    4.9 运行测试子集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../chapter4/4.10-chinese.html">
            
                <a href="../chapter4/4.10-chinese.html">
            
                    
                    4.10 使用测试固件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 配置时和构建时的操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 使用平台无关的文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 配置时运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 构建时运行自定义命令:Ⅰ. 使用add_custom_command
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 构建时运行自定义命令:Ⅱ. 使用add_custom_target
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter5/5.5-chinese.html">
            
                <a href="../chapter5/5.5-chinese.html">
            
                    
                    5.5 构建时为特定目标运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../chapter5/5.6-chinese.html">
            
                <a href="../chapter5/5.6-chinese.html">
            
                    
                    5.6 探究编译和链接命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../chapter5/5.7-chinese.html">
            
                <a href="../chapter5/5.7-chinese.html">
            
                    
                    5.7 探究编译器标志命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../chapter5/5.8-chinese.html">
            
                <a href="../chapter5/5.8-chinese.html">
            
                    
                    5.8 探究可执行命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../chapter5/5.9-chinese.html">
            
                <a href="../chapter5/5.9-chinese.html">
            
                    
                    5.9 使用生成器表达式微调配置和编译
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 生成源码
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 使用Python在配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 构建时使用Python生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 记录项目版本信息以便报告
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter6/6.5-chinese.html">
            
                <a href="../chapter6/6.5-chinese.html">
            
                    
                    6.5 从文件中记录项目版本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../chapter6/6.6-chinese.html">
            
                <a href="../chapter6/6.6-chinese.html">
            
                    
                    6.6 配置时记录Git Hash值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.7" data-path="../chapter6/6.7-chinese.html">
            
                <a href="../chapter6/6.7-chinese.html">
            
                    
                    6.7 构建时记录Git Hash值
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 构建项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 使用函数和宏重用代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 将CMake源代码分成模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 编写函数来测试和设置编译器标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 用指定参数定义函数或宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter7/7.5-chinese.html">
            
                <a href="../chapter7/7.5-chinese.html">
            
                    
                    7.5 重新定义函数和宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter7/7.6-chinese.html">
            
                <a href="../chapter7/7.6-chinese.html">
            
                    
                    7.6 使用废弃函数、宏和变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="../chapter7/7.7-chinese.html">
            
                <a href="../chapter7/7.7-chinese.html">
            
                    
                    7.7 add_subdirectory的限定范围
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="../chapter7/7.8-chinese.html">
            
                <a href="../chapter7/7.8-chinese.html">
            
                    
                    7.8 使用target_sources避免全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="../chapter7/7.9-chinese.html">
            
                <a href="../chapter7/7.9-chinese.html">
            
                    
                    7.9 组织Fortran项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 超级构建模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 使用超级构建模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 使用超级构建管理依赖项:Ⅰ.Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 使用超级构建管理依赖项:Ⅱ.FFTW库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 使用超级构建管理依赖项:Ⅲ.Google Test框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 使用超级构建支持项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="9.0-chinese.html">
            
                <a href="9.0-chinese.html">
            
                    
                    第9章 语言混合项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="9.1-chinese.html">
            
                <a href="9.1-chinese.html">
            
                    
                    9.1 使用C/C++库构建Fortran项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="9.2-chinese.html">
            
                <a href="9.2-chinese.html">
            
                    
                    9.2 使用Fortran库构建C/C++项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="9.3-chinese.html">
            
                <a href="9.3-chinese.html">
            
                    
                    9.3 使用Cython构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="9.4-chinese.html">
            
                <a href="9.4-chinese.html">
            
                    
                    9.4 使用Boost.Python构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="9.5-chinese.html">
            
                <a href="9.5-chinese.html">
            
                    
                    9.5 使用pybind11构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.12.6" data-path="9.6-chinese.html">
            
                <a href="9.6-chinese.html">
            
                    
                    9.6 使用Python CFFI混合C，C++，Fortran和Python
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../chapter10/10.0-chinese.html">
            
                <a href="../chapter10/10.0-chinese.html">
            
                    
                    第10章 编写安装程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../chapter10/10.1-chinese.html">
            
                <a href="../chapter10/10.1-chinese.html">
            
                    
                    10.1 安装项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../chapter10/10.2-chinese.html">
            
                <a href="../chapter10/10.2-chinese.html">
            
                    
                    10.2 生成输出头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../chapter10/10.3-chinese.html">
            
                <a href="../chapter10/10.3-chinese.html">
            
                    
                    10.3 输出目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../chapter10/10.4-chinese.html">
            
                <a href="../chapter10/10.4-chinese.html">
            
                    
                    10.4 安装超级构建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 打包项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 生成源代码和二进制包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 通过PyPI发布使用CMake/pybind11构建的C++/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 通过PyPI发布使用CMake/CFFI构建C/Fortran/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter11/11.4-chinese.html">
            
                <a href="../chapter11/11.4-chinese.html">
            
                    
                    11.4 以Conda包的形式发布一个简单的项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.5" data-path="../chapter11/11.5-chinese.html">
            
                <a href="../chapter11/11.5-chinese.html">
            
                    
                    11.5 将Conda包作为依赖项发布给项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter12/12.0-chinese.html">
            
                <a href="../chapter12/12.0-chinese.html">
            
                    
                    第12章 构建文档
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter12/12.1-chinese.html">
            
                <a href="../chapter12/12.1-chinese.html">
            
                    
                    12.1 使用Doxygen构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter12/12.2-chinese.html">
            
                <a href="../chapter12/12.2-chinese.html">
            
                    
                    12.2 使用Sphinx构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter12/12.3-chinese.html">
            
                <a href="../chapter12/12.3-chinese.html">
            
                    
                    12.3 结合Doxygen和Sphinx
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../chapter13/13.0-chinese.html">
            
                <a href="../chapter13/13.0-chinese.html">
            
                    
                    第13章 选择生成器和交叉编译
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../chapter13/13.1-chinese.html">
            
                <a href="../chapter13/13.1-chinese.html">
            
                    
                    13.1 使用CMake构建Visual Studio 2017项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../chapter13/13.2-chinese.html">
            
                <a href="../chapter13/13.2-chinese.html">
            
                    
                    13.2 交叉编译hello world示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../chapter13/13.3-chinese.html">
            
                <a href="../chapter13/13.3-chinese.html">
            
                    
                    13.3 使用OpenMP并行化交叉编译Windows二进制文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter14/14.0-chinese.html">
            
                <a href="../chapter14/14.0-chinese.html">
            
                    
                    第14章 测试面板
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter14/14.1-chinese.html">
            
                <a href="../chapter14/14.1-chinese.html">
            
                    
                    14.1 将测试部署到CDash
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter14/14.2-chinese.html">
            
                <a href="../chapter14/14.2-chinese.html">
            
                    
                    14.2 CDash显示测试覆盖率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter14/14.3-chinese.html">
            
                <a href="../chapter14/14.3-chinese.html">
            
                    
                    14.3 使用AddressSanifier向CDash报告内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter14/14.4-chinese.html">
            
                <a href="../chapter14/14.4-chinese.html">
            
                    
                    14.4 使用ThreadSaniiser向CDash报告数据争用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter15/15.0-chinese.html">
            
                <a href="../chapter15/15.0-chinese.html">
            
                    
                    第15章 使用CMake构建已有项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter15/15.1-chinese.html">
            
                <a href="../chapter15/15.1-chinese.html">
            
                    
                    15.1 如何开始迁移项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter15/15.2-chinese.html">
            
                <a href="../chapter15/15.2-chinese.html">
            
                    
                    15.2 生成文件并编写平台检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter15/15.3-chinese.html">
            
                <a href="../chapter15/15.3-chinese.html">
            
                    
                    15.3 检测所需的链接和依赖关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.4" data-path="../chapter15/15.4-chinese.html">
            
                <a href="../chapter15/15.4-chinese.html">
            
                    
                    15.4 复制编译标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.5" data-path="../chapter15/15.5-chinese.html">
            
                <a href="../chapter15/15.5-chinese.html">
            
                    
                    15.5 移植测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.6" data-path="../chapter15/15.6-chinese.html">
            
                <a href="../chapter15/15.6-chinese.html">
            
                    
                    15.6 移植安装目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.7" data-path="../chapter15/15.7-chinese.html">
            
                <a href="../chapter15/15.7-chinese.html">
            
                    
                    15.7 进一步迁移的措施
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.8" data-path="../chapter15/15.8-chinese.html">
            
                <a href="../chapter15/15.8-chinese.html">
            
                    
                    15.8 项目转换为CMake的常见问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../chapter16/16.0-chinese.html">
            
                <a href="../chapter16/16.0-chinese.html">
            
                    
                    第16章 可能感兴趣的书
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../chapter16/16.1-chinese.html">
            
                <a href="../chapter16/16.1-chinese.html">
            
                    
                    16.1 留下评论——让其他读者知道你的想法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >9.6 使用Python CFFI混合C，C++，Fortran和Python</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="96-&#x4F7F;&#x7528;python-cffi&#x6DF7;&#x5408;c&#xFF0C;c&#xFF0C;fortran&#x548C;python">9.6 &#x4F7F;&#x7528;Python CFFI&#x6DF7;&#x5408;C&#xFF0C;C++&#xFF0C;Fortran&#x548C;Python</h1>
<p><strong>NOTE</strong>:<em>&#x6B64;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5728; <a href="https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-9/recipe-06" target="_blank">https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-9/recipe-06</a> &#x4E2D;&#x627E;&#x5230;&#xFF0C;&#x5176;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;C++&#x793A;&#x4F8B;&#x548C;&#x4E00;&#x4E2A;Fortran&#x793A;&#x4F8B;&#x3002;&#x8BE5;&#x793A;&#x4F8B;&#x5728;CMake 3.11&#x7248;(&#x6216;&#x66F4;&#x9AD8;&#x7248;&#x672C;)&#x4E2D;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x5DF2;&#x7ECF;&#x5728;GNU/Linux&#x3001;macOS&#x548C;Windows&#x4E0A;&#x8FDB;&#x884C;&#x8FC7;&#x6D4B;&#x8BD5;&#x3002;</em></p>
<p>&#x524D;&#x9762;&#x7684;&#x4E09;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;Cython&#x3001;Boost.Python&#x548C;pybind11&#x4F5C;&#x4E3A;&#x8FDE;&#x63A5;Python&#x548C;C++&#x7684;&#x5DE5;&#x5177;&#x3002;&#x4E4B;&#x524D;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x4E3B;&#x8981;&#x8FDE;&#x63A5;&#x7684;&#x662F;C++&#x63A5;&#x53E3;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x9047;&#x5230;&#x8FD9;&#x6837;&#x7684;&#x60C5;&#x51B5;&#xFF1A;&#x5C06;Python&#x4E0E;Fortran&#x6216;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x8FDB;&#x884C;&#x63A5;&#x53E3;&#x3002;</p>
<p>&#x672C;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;Python C&#x7684;&#x5916;&#x90E8;&#x51FD;&#x6570;&#x63A5;&#x53E3;(CFFI&#xFF0C;&#x53C2;&#x89C1;<a href="https://cffi.readthedocs.io)&#x3002;&#x7531;&#x4E8E;C&#x662F;&#x901A;&#x7528;&#x8BED;&#x8A00;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;(&#x5305;&#x62EC;Fortran)&#x90FD;&#x80FD;&#x591F;&#x4E0E;C&#x63A5;&#x53E3;&#x8FDB;&#x884C;&#x901A;&#x4FE1;&#xFF0C;&#x6240;&#x4EE5;Python" target="_blank">https://cffi.readthedocs.io)&#x3002;&#x7531;&#x4E8E;C&#x662F;&#x901A;&#x7528;&#x8BED;&#x8A00;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;(&#x5305;&#x62EC;Fortran)&#x90FD;&#x80FD;&#x591F;&#x4E0E;C&#x63A5;&#x53E3;&#x8FDB;&#x884C;&#x901A;&#x4FE1;&#xFF0C;&#x6240;&#x4EE5;Python</a> CFFI&#x662F;&#x5C06;Python&#x4E0E;&#x5927;&#x91CF;&#x8BED;&#x8A00;&#x7ED3;&#x5408;&#x5728;&#x4E00;&#x8D77;&#x7684;&#x5DE5;&#x5177;&#x3002;Python CFFI&#x7684;&#x7279;&#x6027;&#x662F;&#xFF0C;&#x751F;&#x6210;&#x7B80;&#x5355;&#x4E14;&#x975E;&#x4FB5;&#x5165;&#x6027;&#x7684;C&#x63A5;&#x53E3;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5B83;&#x65E2;&#x4E0D;&#x9650;&#x5236;&#x8BED;&#x8A00;&#x7279;&#x6027;&#x4E2D;&#x7684;Python&#x5C42;&#xFF0C;&#x4E5F;&#x4E0D;&#x4F1A;&#x5BF9;C&#x5C42;&#x4EE5;&#x4E0B;&#x7684;&#x4EE3;&#x7801;&#x6709;&#x4EFB;&#x4F55;&#x9650;&#x5236;&#x3002;</p>
<p>&#x672C;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x5C06;&#x4F7F;&#x7528;&#x524D;&#x9762;&#x793A;&#x4F8B;&#x7684;&#x94F6;&#x884C;&#x5E10;&#x6237;&#x793A;&#x4F8B;&#xFF0C;&#x901A;&#x8FC7;C&#x63A5;&#x53E3;&#x5C06;Python CFFI&#x5E94;&#x7528;&#x4E8E;Python&#x548C;C++&#x3002;&#x6211;&#x4EEC;&#x7684;&#x76EE;&#x6807;&#x662F;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x4E0A;&#x4E0B;&#x6587;&#x611F;&#x77E5;&#x7684;&#x63A5;&#x53E3;&#x3002;&#x63A5;&#x53E3;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5B9E;&#x4F8B;&#x5316;&#x51E0;&#x4E2A;&#x94F6;&#x884C;&#x5E10;&#x6237;&#xFF0C;&#x6BCF;&#x4E2A;&#x5E10;&#x6237;&#x90FD;&#x5E26;&#x6709;&#x5176;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x901A;&#x8FC7;&#x8BA8;&#x8BBA;&#x5982;&#x4F55;&#x4F7F;&#x7528;Python CFFI&#x6765;&#x8FDE;&#x63A5;Python&#x548C;Fortran&#x6765;&#x7ED3;&#x675F;&#x672C;&#x6559;&#x7A0B;&#x3002;</p>
<p>&#x7B2C;11&#x7AE0;&#x7B2C;3&#x8282;&#x4E2D;&#xFF0C;&#x901A;&#x8FC7;PyPI&#x5206;&#x53D1;&#x4E00;&#x4E2A;&#x7528;CMake/CFFI&#x6784;&#x5EFA;&#x7684;C/Fortran/Python&#x9879;&#x76EE;&#xFF0C;&#x5C4A;&#x65F6;&#x6211;&#x4EEC;&#x5C06;&#x91CD;&#x65B0;&#x8BA8;&#x8BBA;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x5E76;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x6253;&#x5305;&#x5B83;&#xFF0C;&#x4F7F;&#x5B83;&#x53EF;&#x4EE5;&#x7528;<code>pip</code>&#x5B89;&#x88C5;&#x3002;</p>
<h2 id="&#x51C6;&#x5907;&#x5DE5;&#x4F5C;">&#x51C6;&#x5907;&#x5DE5;&#x4F5C;</h2>
<p>&#x6211;&#x4EEC;&#x4ECE;C++&#x5B9E;&#x73B0;&#x548C;&#x63A5;&#x53E3;&#x5F00;&#x59CB;&#xFF0C;&#x628A;&#x5B83;&#x4EEC;&#x653E;&#x5728;&#x540D;&#x4E3A;<code>account/implementation</code>&#x7684;&#x5B50;&#x76EE;&#x5F55;&#x4E2D;&#x3002;&#x5B9E;&#x73B0;&#x6587;&#x4EF6;(<code>cpp_implementation.cpp</code>)&#x7C7B;&#x4F3C;&#x4E8E;&#x4E4B;&#x524D;&#x7684;&#x793A;&#x4F8B;&#xFF0C;&#x4F46;&#x662F;&#x5305;&#x542B;&#x6709;&#x65AD;&#x8A00;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5C06;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x4FDD;&#x6301;&#x5728;&#x4E00;&#x4E2A;&#x4E0D;&#x900F;&#x660E;&#x7684;&#x53E5;&#x67C4;&#x4E2D;&#xFF0C;&#x6240;&#x4EE5;&#x5FC5;&#x987B;&#x786E;&#x4FDD;&#x5BF9;&#x8C61;&#x5728;&#x8BBF;&#x95EE;&#x65F6;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;cpp_implementation.hpp&quot;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cassert&gt;</span></span>

Account::Account()
{
  balance = <span class="hljs-number">0.0</span>;
  is_initialized = <span class="hljs-literal">true</span>;
}
Account::~Account()
{
  assert(is_initialized);
  is_initialized = <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">void</span> Account::deposit(<span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)
{
  assert(is_initialized);
  balance += amount;
}
<span class="hljs-keyword">void</span> Account::withdraw(<span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)
{
  assert(is_initialized);
  balance -= amount;
}
<span class="hljs-keyword">double</span> Account::get_balance() <span class="hljs-keyword">const</span>
{
  assert(is_initialized);
  <span class="hljs-keyword">return</span> balance;
}
</code></pre>
<p>&#x63A5;&#x53E3;&#x6587;&#x4EF6;(<code>cpp_implementation.hpp</code>)&#x5305;&#x542B;&#x5982;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> once</span>

<span class="hljs-keyword">class</span> Account
{
<span class="hljs-keyword">public</span>:
  Account();
  ~Account();
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deposit</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span></span>;
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">withdraw</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span></span>;
  <span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">get_balance</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span></span>;

<span class="hljs-keyword">private</span>:
  <span class="hljs-keyword">double</span> balance;
  <span class="hljs-keyword">bool</span> is_initialized;
};
</code></pre>
<p>&#x6B64;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x9694;&#x79BB;&#x4E86;C-C++&#x63A5;&#x53E3;(<code>c_cpp_interface.cpp</code>)&#x3002;&#x8FD9;&#x5C06;&#x662F;&#x6211;&#x4EEC;&#x4E0E;Python CFFI&#x8FDE;&#x63A5;&#x7684;&#x63A5;&#x53E3;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;account.h&quot;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;cpp_implementation.hpp&quot;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> AS_TYPE(Type, Obj) reinterpret_cast&lt;Type *&gt;(Obj)</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> AS_CTYPE(Type, Obj) reinterpret_cast&lt;const Type *&gt;(Obj)</span>

<span class="hljs-keyword">account_context_t</span> *account_new()
{
  <span class="hljs-keyword">return</span> AS_TYPE(<span class="hljs-keyword">account_context_t</span>, <span class="hljs-keyword">new</span> Account());
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">account_free</span><span class="hljs-params">(account_context_t *context)</span> </span>{ <span class="hljs-function"><span class="hljs-keyword">delete</span> <span class="hljs-title">AS_TYPE</span><span class="hljs-params">(Account, context)</span></span>; }
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">account_deposit</span><span class="hljs-params">(account_context_t *context, <span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span>
</span>{
  <span class="hljs-keyword">return</span> AS_TYPE(Account, context)-&gt;deposit(amount);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">account_withdraw</span><span class="hljs-params">(account_context_t *context, <span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span>
</span>{
  <span class="hljs-keyword">return</span> AS_TYPE(Account, context)-&gt;withdraw(amount);
}
<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">account_get_balance</span><span class="hljs-params">(<span class="hljs-keyword">const</span> account_context_t *context)</span>
</span>{
  <span class="hljs-keyword">return</span> AS_CTYPE(Account, context)-&gt;get_balance();
}
</code></pre>
<p><code>account</code>&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x58F0;&#x660E;&#x4E86;C&#x63A5;&#x53E3;(<code>account.h</code>):</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> ACCOUNT_API</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;account_export.h&quot;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> ACCOUNT_API ACCOUNT_EXPORT</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> __cplusplus</span>
<span class="hljs-keyword">extern</span> <span class="hljs-string">&quot;C&quot;</span>
{
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
  <span class="hljs-keyword">struct</span> account_context;
  <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> account_context <span class="hljs-keyword">account_context_t</span>;
  <span class="hljs-function">ACCOUNT_API
  account_context_t *<span class="hljs-title">account_new</span><span class="hljs-params">()</span></span>;
  <span class="hljs-function">ACCOUNT_API
  <span class="hljs-keyword">void</span> <span class="hljs-title">account_free</span><span class="hljs-params">(account_context_t *context)</span></span>;
  <span class="hljs-function">ACCOUNT_API
  <span class="hljs-keyword">void</span> <span class="hljs-title">account_deposit</span><span class="hljs-params">(account_context_t *context, <span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span></span>;
  <span class="hljs-function">ACCOUNT_API
  <span class="hljs-keyword">void</span> <span class="hljs-title">account_withdraw</span><span class="hljs-params">(account_context_t *context, <span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span></span>;
  <span class="hljs-function">ACCOUNT_API
  <span class="hljs-keyword">double</span> <span class="hljs-title">account_get_balance</span><span class="hljs-params">(<span class="hljs-keyword">const</span> account_context_t *context)</span></span>;
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> __cplusplus</span>
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">/* ACCOUNT_H_INCLUDED */</span></span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x8FD8;&#x63CF;&#x8FF0;&#x4E86;Python&#x63A5;&#x53E3;&#xFF0C;&#x5C06;&#x5728;&#x7A0D;&#x540E;&#x5BF9;&#x6B64;&#x8FDB;&#x884C;&#x8BA8;&#x8BBA;(<code>__init_ _.py</code>):</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> subprocess <span class="hljs-keyword">import</span> check_output
<span class="hljs-keyword">from</span> cffi <span class="hljs-keyword">import</span> FFI
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">from</span> configparser <span class="hljs-keyword">import</span> ConfigParser
<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_lib_handle</span><span class="hljs-params">(definitions, header_file, library_file)</span>:</span>
  ffi = FFI()
  command = [<span class="hljs-string">&apos;cc&apos;</span>, <span class="hljs-string">&apos;-E&apos;</span>] + definitions + [header_file]
  interface = check_output(command).decode(<span class="hljs-string">&apos;utf-8&apos;</span>)
  <span class="hljs-comment"># remove possible \r characters on windows which</span>
  <span class="hljs-comment"># would confuse cdef</span>
  _interface = [l.strip(<span class="hljs-string">&apos;\r&apos;</span>) <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> interface.split(<span class="hljs-string">&apos;\n&apos;</span>)]
  ffi.cdef(<span class="hljs-string">&apos;\n&apos;</span>.join(_interface))
  lib = ffi.dlopen(library_file)
  <span class="hljs-keyword">return</span> lib

<span class="hljs-comment"># this interface requires the header file and library file</span>
<span class="hljs-comment"># and these can be either provided by interface_file_names.cfg</span>
<span class="hljs-comment"># in the same path as this file</span>
<span class="hljs-comment"># or if this is not found then using environment variables</span>
_this_path = Path(os.path.dirname(os.path.realpath(__file__)))
_cfg_file = _this_path / <span class="hljs-string">&apos;interface_file_names.cfg&apos;</span>
<span class="hljs-keyword">if</span> _cfg_file.exists():
  config = ConfigParser()
  config.read(_cfg_file)
  header_file_name = config.get(<span class="hljs-string">&apos;configuration&apos;</span>, <span class="hljs-string">&apos;header_file_name&apos;</span>)
  _header_file = _this_path / <span class="hljs-string">&apos;include&apos;</span> / header_file_name
  _header_file = str(_header_file)
  library_file_name = config.get(<span class="hljs-string">&apos;configuration&apos;</span>, <span class="hljs-string">&apos;library_file_name&apos;</span>)
  _library_file = _this_path / <span class="hljs-string">&apos;lib&apos;</span> / library_file_name
  _library_file = str(_library_file)
<span class="hljs-keyword">else</span>:
  _header_file = os.getenv(<span class="hljs-string">&apos;ACCOUNT_HEADER_FILE&apos;</span>)
  <span class="hljs-keyword">assert</span> _header_file <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>
  _library_file = os.getenv(<span class="hljs-string">&apos;ACCOUNT_LIBRARY_FILE&apos;</span>)
  <span class="hljs-keyword">assert</span> _library_file <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>

_lib = get_lib_handle(definitions=[<span class="hljs-string">&apos;-DACCOUNT_API=&apos;</span>, <span class="hljs-string">&apos;-DACCOUNT_NOINCLUDE&apos;</span>],
                      header_file=_header_file,
                      library_file=_library_file)
<span class="hljs-comment"># we change names to obtain a more pythonic API</span>
new = _lib.account_new
free = _lib.account_free
deposit = _lib.account_deposit
withdraw = _lib.account_withdraw
get_balance = _lib.account_get_balance

__all__ = [
    <span class="hljs-string">&apos;__version__&apos;</span>,
    <span class="hljs-string">&apos;new&apos;</span>,
    <span class="hljs-string">&apos;free&apos;</span>,
    <span class="hljs-string">&apos;deposit&apos;</span>,
    <span class="hljs-string">&apos;withdraw&apos;</span>,
    <span class="hljs-string">&apos;get_balance&apos;</span>,
]
</code></pre>
<p>&#x6211;&#x4EEC;&#x770B;&#x5230;&#xFF0C;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x7684;&#x5927;&#x90E8;&#x5206;&#x5DE5;&#x4F5C;&#x662F;&#x901A;&#x7528;&#x7684;&#x548C;&#x53EF;&#x91CD;&#x7528;&#x7684;&#xFF0C;&#x5B9E;&#x9645;&#x7684;&#x63A5;&#x53E3;&#x76F8;&#x5F53;&#x8584;&#x3002;</p>
<p>&#x9879;&#x76EE;&#x7684;&#x5E03;&#x5C40;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-shell">.
&#x251C;&#x2500;&#x2500; account
&#x2502;    &#x251C;&#x2500;&#x2500; account.h
&#x2502;    &#x251C;&#x2500;&#x2500; CMakeLists.txt
&#x2502;    &#x251C;&#x2500;&#x2500; implementation
&#x2502;    &#x2502;    &#x251C;&#x2500;&#x2500; c_cpp_interface.cpp
&#x2502;    &#x2502;    &#x251C;&#x2500;&#x2500; cpp_implementation.cpp
&#x2502;    &#x2502;    &#x2514;&#x2500;&#x2500; cpp_implementation.hpp
&#x2502;    &#x251C;&#x2500;&#x2500; __init__.py
&#x2502;    &#x2514;&#x2500;&#x2500; test.py
&#x2514;&#x2500;&#x2500; CMakeLists.txt
</code></pre>
<h2 id="&#x5177;&#x4F53;&#x5B9E;&#x65BD;">&#x5177;&#x4F53;&#x5B9E;&#x65BD;</h2>
<p>&#x73B0;&#x5728;&#x4F7F;&#x7528;CMake&#x6765;&#x7EC4;&#x5408;&#x8FD9;&#x4E9B;&#x6587;&#x4EF6;&#xFF0C;&#x5F62;&#x6210;&#x4E00;&#x4E2A;Python&#x6A21;&#x5757;:</p>
<ol>
<li><p>&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x5934;&#x6587;&#x4EF6;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x6839;&#x636E;GNU&#x6807;&#x51C6;&#xFF0C;&#x8BBE;&#x7F6E;&#x7F16;&#x8BD1;&#x5E93;&#x7684;&#x4F4D;&#x7F6E;:</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># define minimum cmake version</span>
<span class="hljs-keyword">cmake_minimum_required</span>(VERSION <span class="hljs-number">3.5</span> FATAL_ERROR)

<span class="hljs-comment"># project name and supported language</span>
<span class="hljs-keyword">project</span>(recipe-<span class="hljs-number">06</span> LANGUAGES CXX)

<span class="hljs-comment"># require C++11</span>
<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD <span class="hljs-number">11</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_EXTENSIONS <span class="hljs-keyword">OFF</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD_REQUIRED <span class="hljs-keyword">ON</span>)

<span class="hljs-comment"># specify where to place libraries</span>
<span class="hljs-keyword">include</span>(GNUInstallDirs)
<span class="hljs-keyword">set</span>(CMAKE_LIBRARY_OUTPUT_DIRECTORY
<span class="hljs-variable">${CMAKE_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span>)
</code></pre>
</li>
<li><p>&#x7B2C;&#x4E8C;&#x6B65;&#xFF0C;&#x662F;&#x5728;<code>account</code>&#x5B50;&#x76EE;&#x5F55;&#x4E0B;&#x5305;&#x542B;&#x63A5;&#x53E3;&#x548C;&#x5B9E;&#x73B0;&#x7684;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># interface and sources</span>
<span class="hljs-keyword">add_subdirectory</span>(account)
</code></pre>
</li>
<li><p>&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x4EE5;&#x6D4B;&#x8BD5;&#x5B9A;&#x4E49;(&#x9700;&#x8981;Python&#x89E3;&#x91CA;&#x5668;)&#x7ED3;&#x675F;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># turn on testing</span>
<span class="hljs-keyword">enable_testing</span>()

<span class="hljs-comment"># require python</span>
<span class="hljs-keyword">find_package</span>(PythonInterp REQUIRED)

<span class="hljs-comment"># define test</span>
<span class="hljs-keyword">add_test</span>(
  NAME
    python_test
  COMMAND
    <span class="hljs-variable">${CMAKE_COMMAND}</span> -E env ACCOUNT_MODULE_PATH=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>
                        ACCOUNT_HEADER_FILE=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/account/account.h
                        ACCOUNT_LIBRARY_FILE=$&lt;TARGET_FILE:account&gt;
   <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/account/test.py
)
</code></pre>
</li>
<li><p><code>account/CMakeLists.txt</code>&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;&#x52A8;&#x6001;&#x5E93;&#x76EE;&#x6807;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_library</span>(account
  SHARED
    plementation/c_cpp_interface.cpp
    implementation/cpp_implementation.cpp
  )

target_include_directories(account
  PRIVATE
    <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>
    <span class="hljs-variable">${CMAKE_CURRENT_BINARY_DIR}</span>
  )
</code></pre>
</li>
<li><p>&#x5BFC;&#x51FA;&#x4E00;&#x4E2A;&#x53EF;&#x79FB;&#x690D;&#x7684;&#x5934;&#x6587;&#x4EF6;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">include</span>(GenerateExportHeader)
generate_export_header(account
  BASE_NAME account
  )
</code></pre>
</li>
<li><p>&#x4F7F;&#x7528;Python-C&#x63A5;&#x53E3;&#x8FDB;&#x884C;&#x5BF9;&#x63A5;:</p>
<pre><code class="lang-shell">$ mkdir -p build
$ cd build
$ cmake ..
$ cmake --build .
$ ctest

Start 1: python_test
1/1 Test #1: python_test ...................... Passed 0.14 sec
100% tests passed, 0 tests failed out of 1
</code></pre>
</li>
</ol>
<h2 id="&#x5DE5;&#x4F5C;&#x539F;&#x7406;">&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h2>
<p>&#x867D;&#x7136;&#xFF0C;&#x4E4B;&#x524D;&#x7684;&#x793A;&#x4F8B;&#x8981;&#x6C42;&#x6211;&#x4EEC;&#x663E;&#x5F0F;&#x5730;&#x58F0;&#x660E;Python-C&#x63A5;&#x53E3;&#xFF0C;&#x5E76;&#x5C06;Python&#x540D;&#x79F0;&#x6620;&#x5C04;&#x5230;C(++)&#x7B26;&#x53F7;&#xFF0C;&#x4F46;Python CFFI&#x4ECE;C&#x5934;&#x6587;&#x4EF6;(&#x793A;&#x4F8B;&#x4E2D;&#x662F;<code>account.h</code>)&#x63A8;&#x65AD;&#x51FA;&#x8FD9;&#x79CD;&#x6620;&#x5C04;&#x3002;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x5411;Python CFFI&#x5C42;&#x63D0;&#x4F9B;&#x63CF;&#x8FF0;C&#x63A5;&#x53E3;&#x7684;&#x5934;&#x6587;&#x4EF6;&#x548C;&#x5305;&#x542B;&#x7B26;&#x53F7;&#x7684;&#x52A8;&#x6001;&#x5E93;&#x3002;&#x5728;&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x96C6;&#x6765;&#x5B9E;&#x73B0;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;&#x8FD9;&#x4E9B;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x5728;<code>__init__.py</code>&#x4E2D;&#x627E;&#x5230;:</p>
<pre><code class="lang-python"><span class="hljs-comment"># ...</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_lib_handle</span><span class="hljs-params">(definitions, header_file, library_file)</span>:</span>
  ffi = FFI()
  command = [<span class="hljs-string">&apos;cc&apos;</span>, <span class="hljs-string">&apos;-E&apos;</span>] + definitions + [header_file]
  interface = check_output(command).decode(<span class="hljs-string">&apos;utf-8&apos;</span>)

  <span class="hljs-comment"># remove possible \r characters on windows which</span>
  <span class="hljs-comment"># would confuse cdef</span>
  _interface = [l.strip(<span class="hljs-string">&apos;\r&apos;</span>) <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> interface.split(<span class="hljs-string">&apos;\n&apos;</span>)]

  ffi.cdef(<span class="hljs-string">&apos;\n&apos;</span>.join(_interface))
  lib = ffi.dlopen(library_file)
  <span class="hljs-keyword">return</span> lib

<span class="hljs-comment"># ...</span>

_this_path = Path(os.path.dirname(os.path.realpath(__file__)))
_cfg_file = _this_path / <span class="hljs-string">&apos;interface_file_names.cfg&apos;</span>
<span class="hljs-keyword">if</span> _cfg_file.exists():
    <span class="hljs-comment"># we will discuss this section in chapter 11, recipe 3</span>
<span class="hljs-keyword">else</span>:
  _header_file = os.getenv(<span class="hljs-string">&apos;ACCOUNT_HEADER_FILE&apos;</span>)
  <span class="hljs-keyword">assert</span> _header_file <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>
  _library_file = os.getenv(<span class="hljs-string">&apos;ACCOUNT_LIBRARY_FILE&apos;</span>)
  <span class="hljs-keyword">assert</span> _library_file <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>
  _lib = get_lib_handle(definitions=[<span class="hljs-string">&apos;-DACCOUNT_API=&apos;</span>, <span class="hljs-string">&apos;-DACCOUNT_NOINCLUDE&apos;</span>],
  header_file=_header_file,
  library_file=_library_file)
<span class="hljs-comment"># ...</span>
</code></pre>
<p><code>get_lib_handle</code>&#x51FD;&#x6570;&#x6253;&#x5F00;&#x5934;&#x6587;&#x4EF6;(&#x4F7F;&#x7528;<code>ffi.cdef</code>)&#x5E76;&#x89E3;&#x6790;&#x52A0;&#x8F7D;&#x5E93;(&#x4F7F;&#x7528;<code>ffi.dlopen</code>)&#x3002;&#x5E76;&#x8FD4;&#x56DE;&#x5E93;&#x5BF9;&#x8C61;&#x3002;&#x524D;&#x9762;&#x7684;&#x6587;&#x4EF6;&#x662F;&#x901A;&#x7528;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x4E0D;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x91CD;&#x7528;&#xFF0C;&#x7528;&#x4E8E;&#x4E0E;Python&#x548C;C&#x6216;&#x4F7F;&#x7528;Python CFFI&#x7684;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x8FDB;&#x884C;&#x63A5;&#x53E3;&#x7684;&#x5176;&#x4ED6;&#x9879;&#x76EE;&#x3002;</p>
<p><code>_lib</code>&#x5E93;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5BFC;&#x51FA;&#xFF0C;&#x8FD9;&#x91CC;&#x6709;&#x4E00;&#x4E2A;&#x989D;&#x5916;&#x7684;&#x6B65;&#x9AA4;&#xFF0C;&#x4F7F;Python&#x63A5;&#x53E3;&#x5728;&#x4F7F;&#x7528;&#x65F6;&#xFF0C;&#x611F;&#x89C9;&#x66F4;&#x50CF;Python:</p>
<pre><code class="lang-python"><span class="hljs-comment"># we change names to obtain a more pythonic API</span>
new = _lib.account_new
free = _lib.account_free
deposit = _lib.account_deposit
withdraw = _lib.account_withdraw
get_balance = _lib.account_get_balance

__all__ = [
  <span class="hljs-string">&apos;__version__&apos;</span>,
  <span class="hljs-string">&apos;new&apos;</span>,
  <span class="hljs-string">&apos;free&apos;</span>,
  <span class="hljs-string">&apos;deposit&apos;</span>,
  <span class="hljs-string">&apos;withdraw&apos;</span>,
  <span class="hljs-string">&apos;get_balance&apos;</span>,
]
</code></pre>
<p>&#x6709;&#x4E86;&#x8FD9;&#x4E2A;&#x53D8;&#x5316;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x4F8B;&#x5B50;&#x5199;&#x6210;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x5F0F;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> account
account1 = account.new()
account.deposit(account1, <span class="hljs-number">100.0</span>)
</code></pre>
<p>&#x53E6;&#x4E00;&#x79CD;&#x9009;&#x62E9;&#x5219;&#x4E0D;&#x90A3;&#x4E48;&#x76F4;&#x89C2;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> account <span class="hljs-keyword">import</span> lib
account1 = lib.account_new()
lib.account_deposit(account1, <span class="hljs-number">100.0</span>)
</code></pre>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5982;&#x4F55;&#x4F7F;&#x7528;API&#x6765;&#x5B9E;&#x4F8B;&#x5316;&#x548C;&#x8DDF;&#x8E2A;&#x4E0A;&#x4E0B;&#x6587;:</p>
<pre><code class="lang-python">account1 = account.new()
account.deposit(account1, <span class="hljs-number">10.0</span>)

account2 = account.new()
account.withdraw(account1, <span class="hljs-number">5.0</span>)
account.deposit(account2, <span class="hljs-number">5.0</span>)
</code></pre>
<p>&#x4E3A;&#x4E86;&#x5BFC;&#x5165;<code>account</code>&#x7684;Python&#x6A21;&#x5757;&#xFF0C;&#x9700;&#x8981;&#x63D0;&#x4F9B;<code>ACCOUNT_HEADER_FILE</code>&#x548C;<code>ACCOUNT_LIBRARY_FILE</code>&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x5C31;&#x50CF;&#x6D4B;&#x8BD5;&#x4E2D;&#x90A3;&#x6837;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME
    python_test
  COMMAND
    <span class="hljs-variable">${CMAKE_COMMAND}</span> -E env ACCOUNT_MODULE_PATH=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>
                        ACCOUNT_HEADER_FILE=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/account/account.h
                        ACCOUNT_LIBRARY_FILE=$&lt;TARGET_FILE:account&gt;
    <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/account/test.py
  )
</code></pre>
<p>&#x7B2C;11&#x7AE0;&#x4E2D;&#xFF0C;&#x5C06;&#x8BA8;&#x8BBA;&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x7528;<code>pip</code>&#x5B89;&#x88C5;&#x7684;Python&#x5305;&#xFF0C;&#x5176;&#x4E2D;&#x5934;&#x6587;&#x4EF6;&#x548C;&#x5E93;&#x6587;&#x4EF6;&#x5C06;&#x5B89;&#x88C5;&#x5728;&#x5B9A;&#x4E49;&#x826F;&#x597D;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x4E0D;&#x5FC5;&#x5B9A;&#x4E49;&#x4EFB;&#x4F55;&#x4F7F;&#x7528;Python&#x6A21;&#x5757;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x8BA8;&#x8BBA;&#x4E86;Python&#x65B9;&#x9762;&#x7684;&#x63A5;&#x53E3;&#x4E4B;&#x540E;&#xFF0C;&#x73B0;&#x5728;&#x770B;&#x4E0B;C&#x7684;&#x63A5;&#x53E3;&#x3002;<code>account.h</code>&#x5185;&#x5BB9;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">struct</span> account_context;
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> account_context <span class="hljs-keyword">account_context_t</span>;

<span class="hljs-function">ACCOUNT_API
account_context_t *<span class="hljs-title">account_new</span><span class="hljs-params">()</span></span>;

<span class="hljs-function">ACCOUNT_API
<span class="hljs-keyword">void</span> <span class="hljs-title">account_free</span><span class="hljs-params">(account_context_t *context)</span></span>;

<span class="hljs-function">ACCOUNT_API
<span class="hljs-keyword">void</span> <span class="hljs-title">account_deposit</span><span class="hljs-params">(account_context_t *context, <span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span></span>;

<span class="hljs-function">ACCOUNT_API
<span class="hljs-keyword">void</span> <span class="hljs-title">account_withdraw</span><span class="hljs-params">(account_context_t *context, <span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> amount)</span></span>;

<span class="hljs-function">ACCOUNT_API
<span class="hljs-keyword">double</span> <span class="hljs-title">account_get_balance</span><span class="hljs-params">(<span class="hljs-keyword">const</span> account_context_t *context)</span></span>;
</code></pre>
<p>&#x9ED1;&#x76D2;&#x53E5;&#x67C4;<code>account_context</code>&#x4F1A;&#x4FDD;&#x5B58;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x3002;<code>ACCOUNT_API</code>&#x5B9A;&#x4E49;&#x5728;<code>account_export.h</code>&#x4E2D;&#xFF0C;&#x7531;<code>account/interface/CMakeLists.txt</code>&#x751F;&#x6210;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">include</span>(GenerateExportHeader)
generate_export_header(account
  BASE_NAME account
  )
</code></pre>
<p><code>account_export.h</code>&#x5934;&#x6587;&#x4EF6;&#x5B9A;&#x4E49;&#x4E86;&#x63A5;&#x53E3;&#x51FD;&#x6570;&#x7684;&#x53EF;&#x89C1;&#x6027;&#xFF0C;&#x5E76;&#x786E;&#x4FDD;&#x8FD9;&#x662F;&#x4EE5;&#x4E00;&#x79CD;&#x53EF;&#x79FB;&#x690D;&#x7684;&#x65B9;&#x5F0F;&#x5B8C;&#x6210;&#x7684;&#xFF0C;&#x5B9E;&#x73B0;&#x53EF;&#x4EE5;&#x5728;<code>cpp_implementation.cpp</code>&#x4E2D;&#x627E;&#x5230;&#x3002;&#x5B83;&#x5305;&#x542B;<code>is_initialized</code>&#x5E03;&#x5C14;&#x53D8;&#x91CF;&#xFF0C;&#x53EF;&#x4EE5;&#x68C0;&#x67E5;&#x8FD9;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#x786E;&#x4FDD;API&#x51FD;&#x6570;&#x6309;&#x7167;&#x9884;&#x671F;&#x7684;&#x987A;&#x5E8F;&#x8C03;&#x7528;&#xFF1A;&#x4E0A;&#x4E0B;&#x6587;&#x5728;&#x521B;&#x5EFA;&#x4E4B;&#x524D;&#x6216;&#x91CA;&#x653E;&#x4E4B;&#x540E;&#x90FD;&#x4E0D;&#x5E94;&#x8BE5;&#x88AB;&#x8BBF;&#x95EE;&#x3002;</p>
<h2 id="&#x66F4;&#x591A;&#x4FE1;&#x606F;">&#x66F4;&#x591A;&#x4FE1;&#x606F;</h2>
<p>&#x8BBE;&#x8BA1;Python-C&#x63A5;&#x53E3;&#x65F6;&#xFF0C;&#x5FC5;&#x987B;&#x4ED4;&#x7EC6;&#x8003;&#x8651;&#x5728;&#x54EA;&#x4E00;&#x7AEF;&#x5206;&#x914D;&#x6570;&#x7EC4;&#xFF1A;&#x6570;&#x7EC4;&#x53EF;&#x4EE5;&#x5728;Python&#x7AEF;&#x5206;&#x914D;&#x5E76;&#x4F20;&#x9012;&#x7ED9;C(++)&#x5B9E;&#x73B0;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x8FD4;&#x56DE;&#x6307;&#x9488;&#x7684;C(++)&#x5B9E;&#x73B0;&#x4E0A;&#x5206;&#x914D;&#x3002;&#x540E;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x9002;&#x7528;&#x4E8E;&#x7F13;&#x51B2;&#x533A;&#x5927;&#x5C0F;&#x4E8B;&#x5148;&#x672A;&#x77E5;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x4F46;&#x8FD4;&#x56DE;&#x5230;&#x5206;&#x914D;&#x7ED9;C(++)&#x7AEF;&#x7684;&#x6570;&#x7EC4;&#x6307;&#x9488;&#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x53EF;&#x80FD;&#x5BFC;&#x81F4;Python&#x5783;&#x573E;&#x6536;&#x96C6;&#x5BFC;&#x81F4;&#x5185;&#x5B58;&#x6CC4;&#x6F0F;&#xFF0C;&#x800C;Python&#x5783;&#x573E;&#x6536;&#x96C6;&#x4E0D;&#x4F1A;&#x201C;&#x67E5;&#x770B;&#x201D;&#x5206;&#x914D;&#x7ED9;&#x5B83;&#x7684;&#x6570;&#x7EC4;&#x3002;&#x6211;&#x4EEC;&#x5EFA;&#x8BAE;&#x8BBE;&#x8BA1;C API&#xFF0C;&#x4F7F;&#x6570;&#x7EC4;&#x53EF;&#x4EE5;&#x5728;&#x5916;&#x90E8;&#x5206;&#x914D;&#x5E76;&#x4F20;&#x9012;&#x7ED9;C&#x5B9E;&#x73B0;&#x3002;&#x7136;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;<code>__init__.py</code>&#x4E2D;&#x5206;&#x914D;&#x8FD9;&#x4E9B;&#x6570;&#x7EC4;&#xFF0C;&#x5982;&#x4E0B;&#x4F8B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> cffi <span class="hljs-keyword">import</span> FFI
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

_ffi = FFI()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">return_array</span><span class="hljs-params">(context, array_len)</span>:</span>
  <span class="hljs-comment"># create numpy array</span>
  array_np = np.zeros(array_len, dtype=np.float64)

  <span class="hljs-comment"># cast a pointer to its data</span>
  array_p = _ffi.cast(<span class="hljs-string">&quot;double *&quot;</span>, array_np.ctypes.data)

  <span class="hljs-comment"># pass the pointer</span>
  _lib.mylib_myfunction(context, array_len, array_p)

  <span class="hljs-comment"># return the array as a list</span>
  <span class="hljs-keyword">return</span> array_np.tolist()
</code></pre>
<p><code>return_array</code>&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Python&#x5217;&#x8868;&#x3002;&#x56E0;&#x4E3A;&#x5728;Python&#x7AEF;&#x5B8C;&#x6210;&#x4E86;&#x6240;&#x6709;&#x7684;&#x5206;&#x914D;&#x5DE5;&#x4F5C;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x5FC5;&#x62C5;&#x5FC3;&#x5185;&#x5B58;&#x6CC4;&#x6F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;&#x7559;&#x7ED9;&#x5783;&#x573E;&#x6536;&#x96C6;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;Fortran&#x793A;&#x4F8B;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x53C2;&#x8003;&#x4EE5;&#x4E0B;Git&#x5E93;:<a href="https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter09/recipe06/Fortran-example" target="_blank">https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter09/recipe06/Fortran-example</a> &#x3002;&#x4E0E;C++&#x5B9E;&#x73B0;&#x7684;&#x4E3B;&#x8981;&#x533A;&#x522B;&#x5728;&#x4E8E;&#xFF0C;account&#x5E93;&#x662F;&#x7531;Fortran 90&#x6E90;&#x6587;&#x4EF6;&#x7F16;&#x8BD1;&#x800C;&#x6210;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x5728;<code>account/CMakeLists.txt</code>&#x4E2D;&#x4F7F;&#x7528;&#x4E86;Fortran 90&#x6E90;&#x6587;&#x4EF6;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_library</span>(account
  SHARED
      implementation/fortran_implementation.f90
  )
</code></pre>
<p>&#x4E0A;&#x4E0B;&#x6587;&#x4FDD;&#x5B58;&#x5728;&#x7528;&#x6237;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;&#x4E2D;:</p>
<pre><code class="lang-fortran"><span class="hljs-keyword">type</span> :: account
  <span class="hljs-keyword">private</span>
  <span class="hljs-keyword">real</span>(<span class="hljs-keyword">c_double</span>) :: balance
  <span class="hljs-keyword">logical</span> :: is_initialized = .false.
<span class="hljs-keyword">end</span> <span class="hljs-keyword">type</span>
</code></pre>
<p>Fortran&#x5B9E;&#x73B0;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>iso_c_binding</code>&#x6A21;&#x5757;&#x89E3;&#x6790;<code>account.h</code>&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x7B26;&#x53F7;&#x548C;&#x65B9;&#x6CD5;:</p>
<pre><code class="lang-fortran"><span class="hljs-keyword">module</span> account_implementation

  <span class="hljs-keyword">use</span>, <span class="hljs-keyword">intrinsic</span> :: <span class="hljs-keyword">iso_c_binding</span>, <span class="hljs-keyword">only</span>: <span class="hljs-keyword">c_double</span>, <span class="hljs-keyword">c_ptr</span>

  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">none</span>

  <span class="hljs-keyword">private</span>

  <span class="hljs-keyword">public</span> account_new
  <span class="hljs-keyword">public</span> account_free
  <span class="hljs-keyword">public</span> account_deposit
  <span class="hljs-keyword">public</span> account_withdraw
  <span class="hljs-keyword">public</span> account_get_balance

  <span class="hljs-keyword">type</span> :: account
    <span class="hljs-keyword">private</span>
    <span class="hljs-keyword">real</span>(<span class="hljs-keyword">c_double</span>) :: balance
    <span class="hljs-keyword">logical</span> :: is_initialized = .false.
  <span class="hljs-keyword">end</span> <span class="hljs-keyword">type</span>

<span class="hljs-keyword">contains</span>

  <span class="hljs-keyword">type</span>(<span class="hljs-keyword">c_ptr</span>) <span class="hljs-function"><span class="hljs-keyword">function</span></span> account_new() <span class="hljs-keyword">bind</span> (c)
    <span class="hljs-keyword">use</span>, <span class="hljs-keyword">intrinsic</span> :: <span class="hljs-keyword">iso_c_binding</span>, <span class="hljs-keyword">only</span>: <span class="hljs-keyword">c_loc</span>
    <span class="hljs-keyword">type</span>(account), <span class="hljs-keyword">pointer</span> :: f_context
    <span class="hljs-keyword">type</span>(<span class="hljs-keyword">c_ptr</span>) :: context

    <span class="hljs-built_in">allocate</span>(f_context)
    context = <span class="hljs-keyword">c_loc</span>(f_context)
    account_new = context
    f_context%balance = <span class="hljs-number">0.0d0</span>
    f_context%is_initialized = .true.
  <span class="hljs-keyword">end</span> <span class="hljs-function"><span class="hljs-keyword">function</span></span>

  <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span> account_free(context) <span class="hljs-keyword">bind</span> (c)
    <span class="hljs-keyword">use</span>, <span class="hljs-keyword">intrinsic</span> :: <span class="hljs-keyword">iso_c_binding</span>, <span class="hljs-keyword">only</span>: <span class="hljs-keyword">c_f_pointer</span>
    <span class="hljs-keyword">type</span>(<span class="hljs-keyword">c_ptr</span>), <span class="hljs-keyword">value</span> :: context
    <span class="hljs-keyword">type</span>(account), <span class="hljs-keyword">pointer</span> :: f_context

    <span class="hljs-keyword">call</span> <span class="hljs-keyword">c_f_pointer</span>(context, f_context)
    <span class="hljs-keyword">call</span> check_valid_context(f_context)
    f_context%balance = <span class="hljs-number">0.0d0</span>
    f_context%is_initialized = .false.
    <span class="hljs-built_in">deallocate</span>(f_context)
  <span class="hljs-keyword">end</span> <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span>

  <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span> check_valid_context(f_context)
    <span class="hljs-keyword">type</span>(account), <span class="hljs-keyword">pointer</span>, <span class="hljs-keyword">intent</span>(<span class="hljs-keyword">in</span>) :: f_context
    <span class="hljs-keyword">if</span> (.not. <span class="hljs-built_in">associated</span>(f_context)) <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">print</span> *, <span class="hljs-string">&apos;ERROR: context is not associated&apos;</span>
        <span class="hljs-keyword">stop</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">end</span> <span class="hljs-keyword">if</span>
    <span class="hljs-keyword">if</span> (.not. f_context%is_initialized) <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">print</span> *, <span class="hljs-string">&apos;ERROR: context is not initialized&apos;</span>
        <span class="hljs-keyword">stop</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">end</span> <span class="hljs-keyword">if</span>
  <span class="hljs-keyword">end</span> <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span>

  <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span> account_withdraw(context, amount) <span class="hljs-keyword">bind</span> (c)
  <span class="hljs-keyword">use</span>, <span class="hljs-keyword">intrinsic</span> :: <span class="hljs-keyword">iso_c_binding</span>, <span class="hljs-keyword">only</span>: <span class="hljs-keyword">c_f_pointer</span>
  <span class="hljs-keyword">type</span>(<span class="hljs-keyword">c_ptr</span>), <span class="hljs-keyword">value</span> :: context
  <span class="hljs-keyword">real</span>(<span class="hljs-keyword">c_double</span>), <span class="hljs-keyword">value</span> :: amount
  <span class="hljs-keyword">type</span>(account), <span class="hljs-keyword">pointer</span> :: f_context

  <span class="hljs-keyword">call</span> <span class="hljs-keyword">c_f_pointer</span>(context, f_context)
  <span class="hljs-keyword">call</span> check_valid_context(f_context)
  f_context%balance = f_context%balance - amount
  <span class="hljs-keyword">end</span> <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span>

  <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span> account_deposit(context, amount) <span class="hljs-keyword">bind</span> (c)
    <span class="hljs-keyword">use</span>, <span class="hljs-keyword">intrinsic</span> :: <span class="hljs-keyword">iso_c_binding</span>, <span class="hljs-keyword">only</span>: <span class="hljs-keyword">c_f_pointer</span>
    <span class="hljs-keyword">type</span>(<span class="hljs-keyword">c_ptr</span>), <span class="hljs-keyword">value</span> :: context
    <span class="hljs-keyword">real</span>(<span class="hljs-keyword">c_double</span>), <span class="hljs-keyword">value</span> :: amount
    <span class="hljs-keyword">type</span>(account), <span class="hljs-keyword">pointer</span> :: f_context

    <span class="hljs-keyword">call</span> <span class="hljs-keyword">c_f_pointer</span>(context, f_context)
    <span class="hljs-keyword">call</span> check_valid_context(f_context)
    f_context%balance = f_context%balance + amount
  <span class="hljs-keyword">end</span> <span class="hljs-function"><span class="hljs-keyword">subroutine</span></span>

  <span class="hljs-keyword">real</span>(<span class="hljs-keyword">c_double</span>) <span class="hljs-function"><span class="hljs-keyword">function</span></span> account_get_balance(context) <span class="hljs-keyword">bind</span> (c)
    <span class="hljs-keyword">use</span>, <span class="hljs-keyword">intrinsic</span> :: <span class="hljs-keyword">iso_c_binding</span>, <span class="hljs-keyword">only</span>: <span class="hljs-keyword">c_f_pointer</span>
    <span class="hljs-keyword">type</span>(<span class="hljs-keyword">c_ptr</span>), <span class="hljs-keyword">value</span>, <span class="hljs-keyword">intent</span>(<span class="hljs-keyword">in</span>) :: context
    <span class="hljs-keyword">type</span>(account), <span class="hljs-keyword">pointer</span> :: f_context

    <span class="hljs-keyword">call</span> <span class="hljs-keyword">c_f_pointer</span>(context, f_context)
    <span class="hljs-keyword">call</span> check_valid_context(f_context)
    account_get_balance = f_context%balance
  <span class="hljs-keyword">end</span> <span class="hljs-function"><span class="hljs-keyword">function</span></span>
<span class="hljs-keyword">end</span> <span class="hljs-keyword">module</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x548C;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x7684;&#x7075;&#x611F;&#x6765;&#x81EA;Armin Ronacher&#x7684;&#x5E16;&#x5B50;&#x201C;Beautiful
Native Libraries&#x201D;&#xFF1A; <a href="http://lucumr.pocoo.org/2013/8/18/beautiful-native-libraries/" target="_blank">http://lucumr.pocoo.org/2013/8/18/beautiful-native-libraries/</a> </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="9.5-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 9.5 使用pybind11构建C++和Python项目">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter10/10.0-chinese.html" class="navigation navigation-next " aria-label="Next page: 第10章 编写安装程序">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"9.6 使用Python CFFI混合C，C++，Fortran和Python","level":"1.12.6","depth":2,"next":{"title":"第10章 编写安装程序","level":"1.13","depth":1,"path":"content/chapter10/10.0-chinese.md","ref":"content/chapter10/10.0-chinese.md","articles":[{"title":"10.1 安装项目","level":"1.13.1","depth":2,"path":"content/chapter10/10.1-chinese.md","ref":"content/chapter10/10.1-chinese.md","articles":[]},{"title":"10.2 生成输出头文件","level":"1.13.2","depth":2,"path":"content/chapter10/10.2-chinese.md","ref":"content/chapter10/10.2-chinese.md","articles":[]},{"title":"10.3 输出目标","level":"1.13.3","depth":2,"path":"content/chapter10/10.3-chinese.md","ref":"content/chapter10/10.3-chinese.md","articles":[]},{"title":"10.4 安装超级构建","level":"1.13.4","depth":2,"path":"content/chapter10/10.4-chinese.md","ref":"content/chapter10/10.4-chinese.md","articles":[]}]},"previous":{"title":"9.5 使用pybind11构建C++和Python项目","level":"1.12.5","depth":2,"path":"content/chapter9/9.5-chinese.md","ref":"content/chapter9/9.5-chinese.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","-sharing","github","pageview-count"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"pageview-count":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":30,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":30,"left":30,"top":30,"bottom":50}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《CMake Cookbook》中文版","language":"zh-hans","gitbook":"*","description":"作为对《CMake Cookbook》英文版的中文翻译。","direction":"ltr"},"file":{"path":"content/chapter9/9.6-chinese.md","mtime":"2020-09-22T10:39:01.983Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-22T11:21:21.825Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

